<?php

/**
 * Messages model
 */
class Messages {
	
	/**
	 * Messages constructor
	 * @param Registry $registry
	 * @return void
	 */
	public function __construct( Registry $registry )
	{
		$this->registry = $registry;
	}
	
	/**
	 * Get a users inbox
	 * @param int $user the user
	 * @return int the cache of messages
	 */
	public function getInbox( $user )
	{
		$sql = "SELECT IF(m.read=0,'unread','read') as read_style, m.subject, m.ID, m.sender, m.recipient,psender.photo,m.message, DATE_FORMAT(m.sent, '%D %M %Y') as sent_time, psender.name as sender_name FROM messages m, profile psender WHERE psender.user_id=m.sender AND m.recipient=" . $user . " ORDER BY m.ID DESC";
		$cache = $this->registry->getObject('db')->cacheQuery( $sql );
		return $cache;
		
	}
	
	
	public function getMessages($id){
	
			$sql = "SELECT  
					r.reply_id,r.msg_id_fk,r.uid_fk,r.profile_id_fk,
					DATE_FORMAT(r.created, '%D %M %Y') as sent_time,
					m.sender,m.recipient,p.name,p.photo
					FROM  
						msg_reply r ,messages m,profile p
					WHERE 
					 r.msg_id_fk=".$id." AND r.msg_id_fk = m.ID and m.sender = p.user_id ORDER BY r.reply_id DESC";
	}
}
?>